package com.sogoodsoft.service.security;

import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Identify {
	public final static String identify="http://www.sogoodsoft.com/Admin_Manager/Return_User_Xml.asp";
	public static String SN = "";
	public static int weekday = 3;
	public static int clock = 11;
	public static int final_count = -1;
	public static int final_limit =4*2;
	public static boolean experience = false;
	public static String charset = "GBK";
	private static int timeout = 30000;
	public static boolean valid = false;
	public static String version_certified ;
	public static String version_from;
	public static String version_to;
	public static String version_name;
	public static boolean busy = false;
	private static boolean done = false;
	private static Calendar calendar;
	
	private static Identify entity = new Identify();
	private static Logger logger = LoggerFactory.getLogger(Identify.class) ;
	
	public static Identify getInstance (){
		return entity;
	}
	
	public void verify(){
		
		calendar = Calendar.getInstance();
		int week = calendar.get(Calendar.DAY_OF_WEEK);
		int cl = calendar.get(Calendar.HOUR_OF_DAY);
		
		
		if(version_certified==null){
			done = false;
			if(!done&&!busy){
				busy = true;
				valid = false;
				Connector connector = new Connector();
				connector.start();
			}
		}
		
		//非正版，试用版过期，每次都验证
		if((!valid)&&(!experience)){
			done = false;
			if(!done&&!busy){
				busy = true;
				valid = false;
				Connector connector = new Connector();
				connector.start();
			}
		}
		
		if(cl==clock&&week==weekday){
			if(!done&&!busy){
				busy = true;
				valid = false;
				Connector connector = new Connector();
				connector.start();
			}
		}else{			
			//非验证日，重置标志，待下次验证
			done = false;
		}
		
		//logger.debug("done="+done+" "+"busy="+busy+" version_certified="+version_certified);

	}
	
	
	class Connector extends Thread{
		public void run(){
			try {
				URL url = new URL(identify);
				HttpURLConnection connect = (HttpURLConnection)url.openConnection();	
				connect.setDoOutput(true);
				connect.setDoInput(true);
				connect.setRequestMethod("POST");
				connect.setReadTimeout(timeout);
				connect.setUseCaches(false);
				String SN = "SN="+Identify.SN;
				String MD5 = "&MD5="+ComputerInfo.specialCode();
				connect.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
				OutputStream out = connect.getOutputStream();
				out.write(SN.getBytes());
				out.write(MD5.getBytes());
				out.flush();
				out.close();
				InputStreamReader in = new InputStreamReader(connect.getInputStream());
				SAXReader reader = new SAXReader(); 
				Document doc = reader.read(in);
				
				Node node = doc.selectSingleNode("//identify/set/week");
				String week = node.getText();
				weekday = Integer.parseInt(week.trim());
				logger.debug("week="+week);
				
				node = doc.selectSingleNode("//identify/set/clock");
				String clock = node.getText();
				Identify.clock = Integer.parseInt(clock.trim());
				logger.debug("clock="+clock);
				
				node = doc.selectSingleNode("//identify/version/certified");
				version_certified = node.getText().trim();
				
				logger.debug("certified="+version_certified);
				
				node = doc.selectSingleNode("//identify/version/name");
				version_name = node.getText();				
				logger.debug("name="+version_name);
				
				node = doc.selectSingleNode("//identify/version/from");
				version_from = node.getText().trim();

				logger.debug("from="+version_from);
				
				node = doc.selectSingleNode("//identify/version/to");
				version_to =node.getText().trim();
				logger.debug("to="+version_to);
				
				if(!Boolean.parseBoolean(version_certified)){
					valid = false;
					
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");					
					Date dateTo = sdf.parse(version_to);	
					if(dateTo.getTime()>new Date().getTime()){
						experience = true;
						final_count=-1;
					}else{
						experience = false;
						final_count++;
					}
				}else{
					valid = true;
				}
					
				
				
//				CharBuffer buffer = CharBuffer.allocate(500);
//				StringBuffer str = new StringBuffer();
//				while(in.read(buffer)>0){					
//					buffer.flip();
//					str.append(buffer.toString());
//				}
//				logger.debug(str.toString());
				in.close();
			} catch (Exception e) {
				final_count++;
				e.printStackTrace();
			} 
			busy = false;
			done = true;
		}
		
	}
	
	
	public static void main(String args[]){
		Identify idt = new Identify();
		idt.verify();
	}
	
}
